package com.wudizaba.microservicesdemoauth.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wudizaba.microservicesdemoauth.module.dto.MicroServicesUserInfoDTO;
import com.wudizaba.microservicesdemoauth.module.dto.MicroservicesAuthRegisterDTO;
import com.wudizaba.microservicesdemoauth.mapper.MicroservicesUserinfoMapper;
import com.wudizaba.microservicesdemoauth.module.po.MicroservicesUserinfo;
import com.wudizaba.microservicesdemoauth.service.IMicroservicesUserinfoService;

import com.wudizaba.microservicesdemocommon.Exceptions.AccountException;
import com.wudizaba.microservicesdemocommon.Exceptions.LoginException;
import com.wudizaba.microservicesdemocommon.util.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wudizaba
 * @since 2024-06-07
 */
@Service
public class MicroservicesUserinfoServiceImpl extends ServiceImpl<MicroservicesUserinfoMapper, MicroservicesUserinfo> implements IMicroservicesUserinfoService {

    /**
     * 登录
     *
     * @return
     */
    @Override
    public String microservicesDemoAuthLogin(MicroServicesUserInfoDTO userinfo) {
        System.out.println("userinfo = " + userinfo);
        String username = userinfo.getUsername();
        String password = userinfo.getPassword();
        // 查询用户信息
        QueryWrapper<MicroservicesUserinfo> eq = Wrappers.<MicroservicesUserinfo>query()
                .eq("username", username);
        MicroservicesUserinfo microservicesUserinfoBySelect = this.getOne(eq);
        // 判断用户是否存在
        if (microservicesUserinfoBySelect == null) {
            throw new LoginException("用户不存在");
        }
        // 加密
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        // 判断密码是否正确
        if (!microservicesUserinfoBySelect.getPassword().equals(password)) {
            throw new AccountException("密码错误");
        }
        // 登录成功
        //登录成功后，生成jwt令牌
        return JwtUtil.createJWT(String.valueOf(microservicesUserinfoBySelect.getId()), microservicesUserinfoBySelect.getUsername(), 1000 * 60 * 60 * 24L);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @Override
    public String microservicesDemoAuthLogout() {
        return "退出成功";
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Override
    public List<MicroservicesUserinfo> microservicesDemoAuthUserinfo() {
        return this.list();
    }

    /**
     * 注册
     *
     * @param userinfo
     * @return
     */
    @Override
    public String microservicesDemoAuthRegister(MicroservicesAuthRegisterDTO userinfo) {
        // 判断用户是否存在
        QueryWrapper<MicroservicesUserinfo> eq = Wrappers.<MicroservicesUserinfo>query()
                .eq("username", userinfo.getUsername());
        MicroservicesUserinfo microservicesUserinfoBySelect = this.getOne(eq);
        if (microservicesUserinfoBySelect != null) {
            throw new LoginException("用户已存在");
        }
        // 加密
        String password = DigestUtils.md5DigestAsHex(userinfo.getPassword().getBytes(StandardCharsets.UTF_8));

        // 封装用户信息
        MicroservicesUserinfo microservicesUserinfo = new MicroservicesUserinfo();
        BeanUtils.copyProperties(userinfo, microservicesUserinfo);

        microservicesUserinfo.setPassword(password);
        microservicesUserinfo.setIsAdmin(false);
        microservicesUserinfo.setIsActive(true);

        this.save(microservicesUserinfo);
        return "注册成功";
    }

    /**
     * 注销
     *
     * @param userinfo
     * @return
     */
    @Override
    public String microservicesDemoAuthCancel(MicroServicesUserInfoDTO userinfo) {
        String password = userinfo.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));

        QueryWrapper<MicroservicesUserinfo> eq = Wrappers.<MicroservicesUserinfo>query().eq("username", userinfo.getUsername());
        MicroservicesUserinfo one = this.getOne(eq);
        // 判断用户是否存在
        // 如果存在，则注销
        if (ObjectUtils.isEmpty(one)) {
            throw new LoginException("用户不存在");
        } else if (!one.getPassword().equals(password)) {
            throw new AccountException("密码错误");
        } else if (!one.getIsActive()) {
            throw new LoginException("用户已注销");
        }
        MicroservicesUserinfo microservicesUserinfo = new MicroservicesUserinfo();
        microservicesUserinfo.setIsActive(false);
        this.update(microservicesUserinfo, null);
        return "注销成功";
    }
}
